﻿using System;
using System.Linq;
using System.Web.Mvc;
using Icms.Core.Model;
using Icms.Core.Repository;
using Omu.Awesome.Mvc;

namespace Icms.WebUI.Controllers
{
    public class MainCaseTypeIdLookupController : LookupController
    {
        private readonly IRepo<CaseType> repo;

        public MainCaseTypeIdLookupController(IRepo<CaseType> repo)
        {
            this.repo = repo;
        }

        public virtual ActionResult Search(string search, int? page = null)
        {
            const int pageSize = 6;
            
            var src =
                repo.Where(o =>o.Title.Contains(search));
            var rows = this.RenderView(@"Awesome\LookupList", src.Skip((page.Value - 1) * pageSize).Take(pageSize));
            return Json(new { rows, more = src.Count() > page * pageSize });
        }

        public ActionResult Get(Guid id)
        {
            return Content((repo.Get(id) ?? new CaseType()).Title);
        }

    }
}